/*
 * @Author: xiawang1024
 * @Date: 2023-04-10 15:41:02
 * @LastEditTime: 2023-04-11 09:10:39
 * @LastEditors: xiawang1024
 * @Description:
 * @FilePath: /rollup-lib/src/utils/weixin.ts
 * 工作，生活，健康
 */
import { postData } from './fetch'
import wx from 'weixin-js-sdk'

export const getOriginWx = () => wx

export const wxSdkInit = (appId) => {
  const url = encodeURIComponent(window.location.href)
  const data = appId ? { url, appId } : { url }
  postData(data).then((res) => {
    const { code, data } = res
    if (code === 0) {
      console.log('获取api配置信息成功！')
      wx.config({
        debug: false,
        appId: data.appId,
        timestamp: data.timestamp,
        nonceStr: data.nonceStr,
        signature: data.signature,
        jsApiList: [
          'updateAppMessageShareData',
          'updateTimelineShareData',
          'onMenuShareWeibo',
          'onMenuShareQZone',
          'chooseImage',
          'previewImage',
          'getNetworkType',
          'openLocation',
          'getLocation',
          'hideOptionMenu',
          'showOptionMenu',
          'hideMenuItems',
          'showMenuItems',
          'hideAllNonBaseMenuItem',
          'showAllNonBaseMenuItem',
          'scanQRCode',
          'chooseWXPay',
          'openProductSpecificView',
        ],
      })
      wx.error(() => {
        console.log('SDK config信息验证失败！')
      })
    } else {
      console.log('获取api配置信息失败！')
    }
  })
}

export const shareInit = ({ title, desc, imgUrl }) => {
  const link = window.location.href
  wx.ready(() => {
    console.log('SDK config信息验证成功！')
    wx.updateAppMessageShareData({
      title,
      desc,
      link,
      imgUrl,
      success: () => {
        console.log(`“分享给朋友”及“分享到QQ”设置成功！`)
      },
    })
    wx.updateTimelineShareData({
      title,
      link,
      imgUrl,
      success: () => {
        console.log(`“分享到朋友圈”及“分享到QQ空间”设置成功！`)
      },
    })
  })
}
